﻿@{
    ViewBag.Title = "CouponGiveAway";
}

@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js?t=Date()")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/shared/shown.js?t=Date()")" type="text/javascript"></script>
}
<style>
    .fileinput-button {
        position: absolute;
        display: inline-block;
        overflow: hidden;
    }

        .fileinput-button input {
            position: absolute;
            right: 0px;
            top: 0px;
            opacity: 0;
            -ms-filter: 'alpha(opacity=0)';
            font-size: 200px;
            width: 100%;
            height: 100%;
        }
</style>
<form id="form1" runat="server" method="post" style="height: 100%">

    <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
        <table width="1000px;">
            <tr>
                <td width="1000px;">
                    <a class="mini-button" id="save" name="save" iconcls="icon-save" onclick="SaveInfo" plain="true">保存</a>
                    <span class="separator"></span>
                    <a class="mini-button" id="close" iconcls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                </td>
            </tr>
        </table>
    </div>
    <table style="margin-top: 10px; margin-left: 25px; border-collapse: collapse;">
        <tr>
            <td width="70px" height="35px" align="right">礼券编码：</td>
            <td width="200px" height="35px" align="left">
                <input name="FNumber" class="mini-textbox" style="width: 200px;" required="true" tabindex="1" />
            </td>
            <td width="80px" height="35px" align="right">领券开始：</td>
            <td width="190px" height="35px" align="left">
                <input class="mini-datepicker" name="FActStartTime" format="yyyy-MM-dd" enabled="true" style="width: 190px;" value="" />                
            </td>
        </tr>
        <tr>
            <td width="70px" height="35px" align="right">礼券类型：</td>
            <td width="190px" height="35px" align="left">
                <table>
                    <tr>
                        <td width="70px">
                            <input id="FCouponNumber" name="FCouponNumber" class="mini-buttonedit" style="width: 70px;"
                                onbuttonclick="getCouponItem('')" onvaluechanged="getCouponValueChanged" selectonfocus="true" />
                        </td>
                        <td width="122px">
                            <input id="FCouponName" name="FCouponName" class="mini-textbox" style="width: 122px; background-color: #CCCCCC;"
                                enabled="false" align="left" readonly="readonly" />
                        </td>
                    </tr>
                </table>
            </td>
            <td width="70px" height="35px" align="right">领券结束：</td>
            <td width="190px" height="35px" align="left">
                <input class="mini-datepicker" name="FActEndTime" format="yyyy-MM-dd" enabled="true" style="width: 190px;" value="" />                
            </td>
        </tr>
        <tr>
            <td width="70px" height ="35px" align="right">使用开始：</td>
            <td width="200px" height ="35px">
                <input class="mini-datepicker" name="FStartTime" format="yyyy-MM-dd" style="width: 190px;" value="" />
            </td>
            <td width="70px" height="35px" align="right">截止使用：</td>
            <td width="190px" height="35px" align="left">
                <input class="mini-datepicker" name="FEndTime" format="yyyy-MM-dd" style="width: 190px;"/>
            </td>
        </tr>
        <tr>
            <td width="70px" height ="35px" align="right">礼券类型：</td>
            <td width="200px" height ="35px">
                <input id="FCouponType" name="FCouponType" class="mini-combobox" style="width:200px;" TabIndex="6" value ="1" data ="dCouponType"/>
            </td>
            <td width="70px" height="35px" align="right">发放日期：</td>
            <td width="190px" height="35px" align="left">
                <input class="mini-datepicker" name="FSendTime" format="yyyy-MM-dd" style="width: 190px;" value="" />
            </td>
        </tr>
        <tr>
            <td width="70px" height="35px" align="right">礼券面额：</td>
            <td width="200px" height="35px" align="left">
                <input name="FAmount" class="mini-spinner" style="width: 200px;" minvalue="0" maxvalue="9999999" showbutton="false" required="true"
                    changeonmousewheel="false" value="0" tabindex="3" /></td>
            <td width="70px" height="35px" align="right">礼券状态：</td>
            <td width="190px" height="35px" align="left">
                <input name="FStatus" class="mini-combobox" style="width: 190px;" value="-1"
                    shownullitem="false" required="true" allowinput="false" align="left" data="CStatus" tabindex="4" />
            </td>
        </tr>
        <tr>
            <td width="70px" height="35px" align="right">发放人：</td>
            <td width="190px" height="35px" align="left">
            @Html.Partial("~/Views/Modular/LookupUserInfo.cshtml")
            <td width="70px" height="35px" align="right">消费时间：</td>
            <td width="190px" height="35px" align="left">
                <input class="mini-datepicker" name="FUseTime" enabled="false" format="yyyy-MM-dd" style="width: 190px;" value="" />
            </td>
        </tr>
        <tr>
            <td width="70px" height="35px" align="right">绑定会员：</td>
            <td width="190px" height="35px" align="left">
            @Html.Partial("~/Views/Modular/LookupMember.cshtml")
            <td width="70px" height="35px" align="right">单据编号：</td>
            <td width="200px" height="35px" align="left">
                <input name="FBillNo" class="mini-textbox" enabled="false" style="width: 190px;" tabindex="6" /></td>
        </tr>
        <tr>
            <td width="70px" height="35px" align="right">备注：</td>
            <td width="200px" height="35px" align="left">
                <input name="FRemark" class="mini-textbox" style="width: 200px;" tabindex="6" /></td>
            <td width="70px" height="35px" align="right"></td>
            <td width="190px" height="35px" align="left">
                <a name="FSendFlag" class="mini-checkbox" style="width: 100px; margin-left: 10px;">是否启用</a>
                <a name="SaveNew" class="mini-checkbox" style="width: 100px; margin-left: 10px;">连续新增</a>
            </td>
        </tr>
    </table>
    <input name="FItemID" class="mini-hidden" />
</form>

<script>
    var CStatus = [{ "id": "-1", "text": "已停用" }, { "id": "0", "text": "已发放" },
                    { "id": "1", "text": "已使用" }];

    var dCouponType = [{ "id": "1", "text": "满减券" },
               { "id": "2", "text": "折扣券" },
            { "id": "3", "text": "体验券" }];

    function FormShown() {
        mini.parse();
        mini.getbyName("FSendFlag").setChecked(true);
        var now = new Date();
    }

    //加载资料
    function SetInfoData(data) {
        if (data.action == "new") {
            //跨页面传递的数据对象，克隆后才可以安全使用
            NewInfo();
        }
        else {
            $.post("GetCouponSend", { mjson: "[{'FItemID':'" + data.FItemID + "','mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
                var form = new mini.Form("form1");
                var mdata = mini.decode(data);
                form.setData(mdata.data[0]);
                mini.getbyName("FCouponNumber").setValue(mdata.data[0].CouponFItemID);
                mini.getbyName("FCouponNumber").setText(mdata.data[0].CouponFNumber);
                mini.getbyName("FCouponName").setValue(mdata.data[0].CouponName);
                mini.getbyName("FUserID").setValue(mdata.data[0].UserFItemID);
                mini.getbyName("FUserID").setText(mdata.data[0].UserFNumber);
                mini.getbyName("FUserName").setValue(mdata.data[0].UserName);
                mini.getbyName("FMemberNumber").setValue(mdata.data[0].MemberFItemID);
                mini.getbyName("FMemberNumber").setText(mdata.data[0].MemberNumber);
                mini.getbyName("FMemberName").setValue(mdata.data[0].MemberName);
                mini.getbyName("FSendFlag").setChecked(true);
                if (mdata.data[0].FStatus == 1) {
                    setEnabled(false);
                }
            });
        }
    }

    function setEnabled(b) {
        mini.getbyName("FCouponNumber").setEnabled(b);
        mini.getbyName("FCouponName").setEnabled(b);
        mini.getbyName("FUserID").setEnabled(b);
        mini.getbyName("FUserName").setEnabled(b);
        mini.getbyName("FMemberNumber").setEnabled(b);
        mini.getbyName("FMemberName").setEnabled(b);
        mini.getbyName("FNumber").setEnabled(b);
        mini.getbyName("FActStartTime").setEnabled(b);
        mini.getbyName("FActEndTime").setEnabled(b);
        mini.getbyName("FAmount").setEnabled(b);
        mini.getbyName("FStatus").setEnabled(b);
        mini.getbyName("FSendTime").setEnabled(b);
        mini.getbyName("FUseTime").setEnabled(b);
        mini.getbyName("FBillNo").setEnabled(b);
        mini.getbyName("FRemark").setEnabled(b);
        mini.getbyName("save").setEnabled(b);
    }

    //新增资料
    function NewInfo() {
        mini.getbyName("FItemID").setValue("0");
        mini.getbyName("FNumber").setValue("");
        mini.getbyName("FStatus").setValue("0");
        mini.getbyName("FRemark").setValue("");
        mini.getbyName("FBillNo").setValue("");
        mini.getbyName("FRemark").setValue("");
        var current = new Date();
        mini.getbyName("FStartTime").setValue(current);
        mini.getbyName("FActStartTime").setValue(current);
        mini.getbyName("FSendTime").setValue(current);
        current.setFullYear(current.getFullYear() + 10);
        mini.getbyName("FEndTime").setValue(current);
        mini.getbyName("FActEndTime").setValue(current);
        mini.getbyName("FUseTime").setValue('');
        mini.getbyName("FMemberNumber").setValue('');
        mini.getbyName("FMemberNumber").setText('');
        mini.getbyName("FMemberName").setValue('');
        $.post('NewCouponSend', { json: '', mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            mini.getbyName("FNumber").setValue(data);
        });
    }

    //保存资料
    function SaveInfo() {
        var form = new mini.Form("#form1");
        form.validate();
        if (form.isValid() == false) return;
        var data = form.getData(true, false);
        var errorMsg = "";
        if (data.FCouponNumber == "") {
            mini.alert("礼券类型没有选择");
            return;
        }
        if (mini.getbyName("FStatus").getValue() >= 0 && mini.getbyName("FMemberNumber").getValue() == '') {
            mini.alert('未绑定会员');
            return;
        }
        data.mSystemAppID = parent.window.getSystemAppID();
        var json = mini.encode(data);
        $.post("SaveCouponSend", { mjson: json }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                if (mini.getbyName("SaveNew").getValue() == "true")
                    NewInfo();
                else
                    CloseWindow();
            }
            else {
                mini.alert("异常:" + ret.error);
            }
        });
    }

    function getCouponItem(mSearchKey) {
        mini.open({
            url: "../GetItem/Index",
            title: "礼券类型选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "Coupon", sSearchKey: mSearchKey };
                iframe.contentWindow.SetData(data);//打开GetItemID.js的SetData
            },
            ondestroy: function (action) {
                if (action.length > 0) {
                    mini.getbyName("FCouponNumber").setValue(action[0].FItemID);
                    mini.getbyName("FCouponNumber").setText(action[0].FNumber);
                    mini.getbyName("FCouponName").setValue(action[0].FName);
                    mini.getbyName("FAmount").setValue(action[0].FValue);
                    getCouponDate(action[0].FItemID);
                }
            }
        });
    }

    function getCouponValueChanged() {
        var sSearchKey = mini.get("FCouponNumber").getText();
        var mAppID = parent.window.getSystemAppID();
        if (sSearchKey == "") return;
        var sTbName = "Coupon";
        var json = "[{";
        json = json + "'FSystemAppID':" + "'" + mAppID + "',";
        json = json + "'FTbName':" + "'" + sTbName + "',";
        json = json + "'FClassID':" + "'0',";
        json = json + "'FNumber':" + "'" + sSearchKey + "'";
        json = json + "}]";
        $.post("../GetItem/GetItemList", { datajson: json }, function (text) {
            var sdata = mini.decode(text);
            if (sdata.data.length == 1) {
                mini.get("FCouponNumber").setValue(sdata.data[0].FItemID);
                mini.get("FCouponNumber").setText(sdata.data[0].FNumber);
                mini.get("FCouponName").setValue(sdata.data[0].FName);
                var json = "[{'FItemID':'" + sdata.data[0].FItemID + "','mSystemAppID':'" + mAppID + "'}]";
                $.post("../Coupon/GetCoupon", { json: json }, function (data) {
                    var mdata = mini.decode(data);
                    mini.getbyName("FAmount").setValue(mdata.data[0].FValue);
                });
                getCouponDate(sdata.data[0].FItemID);
            }
            else {
                mini.get("FCouponNumber").setValue("");
                mini.get("FCouponNumber").setText("");
                mini.get("FCouponName").setValue("");
            }
        });
    }

    function getCouponDate(CouponID) {
        $.post("GetCoupon", { json: "[{'FItemID':'" + CouponID + "','mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
            var mdata = mini.decode(data);
            var current = new Date();
            mini.getbyName("FStartTime").setValue(current);
            if (mdata.data[0].FDateType == 0 || mdata.data[0].FDateType == null) {
                current.setFullYear(current.getFullYear() + 10);
                mini.getbyName("FEndTime").setValue(current);
            } else if (mdata.data[0].FDateType == 1) {
                var dayTime = mdata.data[0].FDays * 24 * 60 * 60 * 1000; //参数天数的时间戳
                var nowTime = current.getTime(); //当天的时间戳
                var t = new Date(nowTime + dayTime).toLocaleString(); //把两个时间戳转换成普通时间
                mini.getbyName("FEndTime").setValue(t);
            } else if (mdata.data[0].FDateType == 2) {
                mini.getbyName("FEndTime").setValue(mdata.data[0].FEndDate);
            }
        });
    }

    $(function () {

        $("#myfile").change(function () {
            var formData = new FormData();
            formData.append("myfile", document.getElementById('myfile').files[0]);
            formData.append("mSystemAppID", parent.window.getSystemAppID());
            $.ajax({
                url: "/Upload/WxUploadFile",
                type: "post",
                data: formData,
                contentType: false,
                processData: false,
                success: function (res) {
                    if (res.status == 1) {
                        $("#Image").attr("src", res.path);
                        mini.get("FLogoUrl").setValue(res.logoUrl);
                    }
                }
            });
        });

    });
</script>
